package com.alex.config;

import com.google.common.util.concurrent.RateLimiter;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.exception.ZuulException;

import static com.google.common.util.concurrent.RateLimiter.*;

/**
 * 限流
 *
 * @author Alex.shu
 */
public class LimitFilter extends ZuulFilter {

    public static volatile RateLimiter rateLimiter = create(500.0);

    @Override
    public String filterType() {
        return  "pre";
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    public boolean shouldFilter() {
        return true;
    }

    public Object run() throws ZuulException {
        // 限流最多同时只能通过500个请求.
        rateLimiter.acquire();
        return null;
    }
}
